﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace FPC.Offer.Service
{
    public abstract class BaseService<R, T>
        where R : IRepository.IRepository<T>
        where T : class
    {
        protected readonly R dal = RepositoryFactory.RepositoryFactory<R>.CreateRepository();

        /// <summary> 
        /// 根据过滤条件，获取记录
        /// </summary>
        /// <param name="exp">lambda 表达式条件 The exp.</param>
        public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)
        {
            return dal.Find (exp);
        }

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="exp">lambda 表达式条件</param>
        /// <returns></returns>
        public bool IsExist(Expression<Func<T, bool>> exp)
        {
            return dal.IsExist(exp);
        }

        /// <summary>
        /// 查找单条数据
        /// </summary>
        /// <param name="exp">lambda 表达式条件</param>
        /// <returns></returns>
        public T FindSingle(Expression<Func<T, bool>> exp)
        {
            return dal.FindSingle(exp);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<T> Select()
        {
            return dal.Select();
        }

        /// <summary>
        /// 通过条件查询满足所有
        /// </summary>
        /// <param name="exp">lambda 表达式条件</param>
        /// <returns></returns>
        public List<T> Select(Expression<Func<T, bool>> exp)
        {
            return dal.Select(exp);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="totalSize">总条数</param>
        /// <returns></returns>
        public List<T> SelectPage(int pageIndex, int pageSize, out long totalSize)
        {
            return dal.SelectPage(pageIndex, pageSize, out totalSize);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="exp">lambda 表达式条件</param>
        /// <param name="totalSize">总条数</param>
        /// <returns></returns>
        public List<T> SelectPage(int pageIndex, int pageSize, Expression<Func<T, bool>> exp, out long totalSize)
        {
            return dal.SelectPage(pageIndex, pageSize, exp, out totalSize);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="exp">lambda 表达式Where条件</param>
        /// <param name="order">lambda 表达式排序条件</param>
        /// <param name="orderType">true 是正序 false 则相反</param>
        /// <param name="totalSize">总条数</param>
        /// <returns></returns>
        public List<T> SelectPageOrder(int pageIndex, int pageSize, Expression<Func<T, bool>> exp, Expression<Func<T, object>> order, bool orderType, out long totalSize)
        {
            return dal.SelectPageOrder(pageIndex, pageSize, exp, order, orderType, out totalSize);
        }

        /// <summary>
        /// 通用单实体添加
        /// </summary>
        /// <param name="entity">实体</param>
        public int Add(T entity)
        {
            return dal.Add(entity);
        }

        /// <summary>
        /// 通用单实体批量添加
        /// </summary>
        /// <param name="entities">实体集合</param>
        public int Add(List<T> entities)
        {
            return dal.Add(entities);
        }

        /// <summary>
        /// 通用单实体修改
        /// </summary>
        /// <param name="entity">实体</param>
        public int Update(T entity)
        {
            return dal.Update(entity);
        }

        /// <summary>
        /// 通用单实体删除
        /// </summary>
        /// <param name="entity">实体</param>
        public int Delete(T entity)
        {
            return dal.Delete(entity);
        }

        /// <summary>
        /// DBContext上下文中的缓存与数据库进行同步保存
        /// </summary>
        public int SaveChanges()
        {
            return dal.SaveChanges();
        }
    }
}
